﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using BEGDC.CC.Web.UserControls;
using BEGDC.CC.BL.CCFlow.CR;
using BEGDC.CC.BL.Common.Query;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.BL.Common;
using BEGDC.Utility.Castle;
using BEGDC.CC.Web.Control;

namespace BEGDC.CC.Web.Common
{
	/// <summary>
	/// Summary description for AccompanyInput.
	/// </summary>
	public class CISSearchForCV : System.Web.UI.Page
	{
		#region Variables
		protected System.Web.UI.WebControls.ImageButton Btn_Save;
		protected System.Web.UI.WebControls.ImageButton Btn_Close;
		protected System.Web.UI.WebControls.ImageButton btnPageLast;
		protected System.Web.UI.WebControls.ImageButton btnPageNext;
		protected System.Web.UI.WebControls.ImageButton btnPagePrev;
		protected System.Web.UI.WebControls.ImageButton btnPageFirst;
		protected System.Web.UI.WebControls.DataGrid DG_CustomerList;
		//private DataTable CISSource;
		#endregion

		#region Properties
		private DataTable CachedFilterTable
		{
			set
			{
				Session["cachedFilterTable"] = value;
			}
			get
			{
				if(Session["cachedFilterTable"] == null)
					return null;
				return (DataTable)Session["cachedFilterTable"];
			}
		}
		private string SortExpression
		{
			set
			{
				ViewState["sortExpression"] = value;
			}
			get
			{
				if(ViewState["sortExpression"] == null)
					return string.Empty;
				return (string)ViewState["sortExpression"];
			}
		}
		
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			string CISNameStr = Request.QueryString["CISName"];
			string TitleStr = Request.QueryString["CISTitle"];
			string CompanyStr = Request.QueryString["CISCompany"];
			string isDomestic= Request.QueryString["IsDomestic"];

			if(CISNameStr==""&&TitleStr==""&&CompanyStr=="")
			{
				Page.RegisterClientScriptBlock("","<script>alert('Please specify information about the customer');</script>");
				return;
			}
			else
			{
				this.CISName = CISNameStr;
				this.CISTitle = TitleStr;
				this.CISCompany = CompanyStr;
				if (isDomestic.ToLower()=="yes")
					this.IsDomestic=true;
				else
					this.IsDomestic=false;
			}

            //?????
			string CustomerAttendeeStr = Request["CustomerAttendeeId"];

			if(!this.IsPostBack)
			{
				InitForm(1);
				//this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";
				this.Btn_Close.Attributes["onclick"] = "javascript:OnClose();";
				this.Btn_Save.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/bbw_ok.gif";
				this.Btn_Close.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/bbw_close.gif";

			}
		}

		#region CIS Properties
		protected string CISName
		{
			get{ return this.ViewState["CISName"].ToString(); }
			set{ this.ViewState["CISName"] = value; }
		}
		protected string CISTitle
		{
			get{ return this.ViewState["CISTitle"].ToString(); }
			set{ this.ViewState["CISTitle"] = value; }
		}
		protected string CISCompany
		{
			get{ return this.ViewState["CISCompany"].ToString(); }
			set{ this.ViewState["CISCompany"] = value; }
		}
		protected bool IsDomestic
		{
			get{ return (bool)this.ViewState["IsDomestic"]; }
			set{ this.ViewState["IsDomestic"] = value; }
		}
		#endregion

		#region InitForm
		private void InitForm(int pageIndex)
		{
			IQuery QueryService = AppContainer.Instance[ServiceConst.QueryService] as IQuery;
			DataTable CISSource = QueryService.GetCustomerFromCIS(this.CISName,this.CISTitle,this.CISCompany,this.IsDomestic);

			int count = CISSource.DefaultView.Count;
			this.DG_CustomerList.DataSource = CISSource.DefaultView;
			this.DG_CustomerList.DataBind();

			SetPageCount(count, pageIndex);
			//			if (count!=0)
			//				this.Btn_Save.Attributes["onclick"] = "javascript:OnSave();";

		}
		#endregion
	
		public void BindSortedDataGrid()
		{
			string sortExpression = this.SortExpression;
			string sortDeriction = "ASC";
			string lastColumn = string.Empty;
			DataView dv = new DataView(this.CachedFilterTable);
			if(sortExpression != string.Empty)
			{
				lastColumn = sortExpression.Substring(0, sortExpression.IndexOf(","));
				sortDeriction = sortExpression.Substring(sortExpression.IndexOf(",")+1);
				dv.Sort = lastColumn+" "+sortDeriction;
			}

			this.DG_CustomerList.DataSource = dv;
			this.DG_CustomerList.DataBind();
		}

		
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.DG_CustomerList.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.CustomerList_PageIndexChanged);
			this.btnPageFirst.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageFirst_Click);
			this.btnPagePrev.Click += new System.Web.UI.ImageClickEventHandler(this.btnPagePrev_Click);
			this.btnPageNext.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageNext_Click);
			this.btnPageLast.Click += new System.Web.UI.ImageClickEventHandler(this.btnPageLast_Click);
			this.Btn_Save.Click += new System.Web.UI.ImageClickEventHandler(this.Btn_Save_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		#region ---------- paging events ----------------
		private void btnPageFirst_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			this.DG_CustomerList.CurrentPageIndex = 0;
			this.InitForm(1);
		}

		private void btnPagePrev_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			if( _CurrentPageIndex == 1 ) return;
			this.DG_CustomerList.CurrentPageIndex = _CurrentPageIndex-2;
			this.InitForm(_CurrentPageIndex - 1);
		}

		private void btnPageNext_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			if( _CurrentPageIndex == _PageCount ) return;	
			this.DG_CustomerList.CurrentPageIndex = _CurrentPageIndex;
			this.InitForm( _CurrentPageIndex + 1 );
		}

		private void btnPageLast_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			this.DG_CustomerList.CurrentPageIndex = _PageCount-1;
			this.InitForm( _PageCount );
		}
		#endregion

		#region page navigation
		private int _PageCount
		{
			get
			{
				if( ViewState["PageCount"] != null ) return (int) ViewState["PageCount"];
				else
					return 0;
			}
			set
			{
				ViewState["PageCount"] = value;
			}
		}
		private int _CurrentPageIndex
		{
			get
			{
				if( ViewState["CurrentPageIndex"] != null ) return (int) ViewState["CurrentPageIndex"];
				else
					return 0;
			}
			set
			{
				ViewState["CurrentPageIndex"] = value;
			}
		}
		private int _RowCount
		{
			get
			{
				if( ViewState["RowCount"] != null ) return (int) ViewState["RowCount"];
				else
					return 0;
			}
			set
			{
				ViewState["RowCount"] = value;
			}
		}
		private void SetPageNavi( int newPage )
		{
			if( newPage <= 1 )
			{
				this.btnPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_first_inact.gif";
				this.btnPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_pre_inact.gif";
				this.btnPageFirst.Attributes["disabled"] = "true";
				this.btnPagePrev.Attributes["disabled"] = "true";
			}
			else
			{
				this.btnPageFirst.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_first.gif";
				this.btnPagePrev.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_pre.gif";
				this.btnPageFirst.Attributes.Remove("disabled");
				this.btnPagePrev.Attributes.Remove("disabled");
			}

			if( newPage >= _PageCount )
			{
				this.btnPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_next_inact.gif";
				this.btnPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_last_inact.gif";
				this.btnPageNext.Attributes["disabled"] = "true";
				this.btnPageLast.Attributes["disabled"] = "true";
			}
			else
			{
				this.btnPageNext.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_next.gif";
				this.btnPageLast.ImageUrl = BEGDC.CC.Web.UserControls.WebUtility.Path+"/images/pg_last.gif";
				this.btnPageNext.Attributes.Remove("disabled");
				this.btnPageLast.Attributes.Remove("disabled");
			}

			//			this.lblCurrentPageIndex.Text = newPage.ToString();
			//			this.lblPageCount.Text = _PageCount.ToString();
			//			this.lblRecordCount.Text = _RowCount.ToString();

		}
		#endregion

		#region SetPageCount
		private void SetPageCount(int count, int pageIndex)
		{
			if( count > 0 )
			{
				_RowCount = count;
				_PageCount = count / this.DG_CustomerList.PageSize;
				if( count % this.DG_CustomerList.PageSize > 0) _PageCount ++;

				_CurrentPageIndex = pageIndex;	
				SetPageNavi( pageIndex );
			}	
			else
			{
				_RowCount = count;
				_PageCount = count;
				_CurrentPageIndex = 0;
				SetPageNavi( 0 );
			}
		}
		#endregion

		#region Save
		private void Btn_Save_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			string temp = string.Empty;
			try
			{
				foreach(DataGridItem row in this.DG_CustomerList.Items)
				{
					RadioButton checkbox = (RadioButton)row.FindControl("CB_CIS");
					if(checkbox.Checked)
					{
						temp = row.Cells[1].Text+"|"+row.Cells[2].Text+"|"+row.Cells[3].Text;
						break;
					}
				}
				temp = WebUtility.EnCodeString(Server,temp);
				if (temp!=string.Empty && temp!="")
					Response.Write("<script>window.close();window.returnValue='"+temp+"';</script>");
				else
					Response.Write("<script>window.close();window.returnValue='';</script>");

			}
			catch(Exception exp)
			{
			}
			
		}
		#endregion


		#region CustomerList_PageIndexChanged
		private void CustomerList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			try
			{
				//SetCheckedProjectList();
				this.DG_CustomerList.CurrentPageIndex = e.NewPageIndex;
				this.InitForm(e.NewPageIndex);
			}
			catch
			{
				WebUtility.Go2ErrorPage(this.Page, BEGDC.CC.BL.Common.ErrorCode.Other_Exception);
			}
		}
		#endregion

      

	}
}
